/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.hsos.samcomm.userManagement.entity;

import de.hsos.samcomm.common.constraints.EMail;
import de.hsos.samcomm.forumManagement.entity.Forum;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.io.Serializable;
import javax.persistence.Column;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * Entität eines Benutzers.
 *
 * @author Dennis
 */
@Entity(name="members")
public class Member implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String OID;
    @Size(min=1)
    @Column(name = "nickname", nullable = false, length = 32)
    private String nickname;

    @NotNull
    @Column(name = "user_password", nullable = false, length = 128)
    @Size(min=1)
    private String password;
    @EMail
    @Size(min=3)
    private String email;
    @NotNull
    @Column(name = "user_group", nullable = false, length = 32)
    private String userGroup;


    @ManyToMany
    private List<Forum> subscriptions = new ArrayList<Forum>();

    /**
     * Default Konstruktor.
     */
    public Member() {}

    /**
     * überladener Konstruktor.
     *
     * @param nickname
     * @param email
     * @param password
     */
    public Member(String nickname, String email, String password) {
        this.nickname = nickname;
        this.email = email;
        this.password = password;
        this.userGroup = UserGroup.USER.name();
    }

    /**
     * Get-Methode für die ObjektID.
     *
     * @return the OID
     */
    public String getOID() {
        return this.OID;
    }

    /**
     * Set-Mehtode für die ObjektID.
     *
     * @param OID the OID to set
     */
    public void setOID(String OID) {
        this.OID = OID;
    }

    /**
     * Get-Methode für den Nickname.
     *
     * @return nickname
     */
    public String getNickname() {
        return nickname;
    }

    /**
     * Set-Methode für den Nickname.
     *
     * @param nickname
     */
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    /**
     * Get-Methode für das Passwort.
     *
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * Set-Methode für das Passwort.
     *
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Get-Methode für die E-Mail.
     *
     * @return email
     */
    public String getEmail() {
        return email;
    }

    /**
     * Set-Methode für die E-Mail.
     *
     * @param email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Get-Methode für die abonnierten Foren.
     *
     * @return subscriptions
     */
    public List<Forum> getSubscriptions() {
        return (List<Forum>)(List<?>)subscriptions;
    }

    /**
     * Set-Methode für die abonnierten Foren.
     *
     * @param subscriptions
     */
    public void setSubscriptions(List<Forum> subscriptions) {
        this.subscriptions = (List<Forum>)(List<?>)subscriptions;
    }

    /**
     * Fügt den abonnierten Foren das übergebene Forum ein.
     *
     * @param f
     */
    public void addSubscription(Forum f) {
        this.subscriptions.add(f);
    }

    /**
     * überschriebene toString-Methode. Gibt den Namen des Benutzers zurück.
     *
     * @return nickname
     */
    @Override
    public String toString() {
        return nickname;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 19 * hash + (this.email != null ? this.email.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Member other = (Member) obj;
        if ((this.nickname == null) ? (other.nickname != null) : !this.nickname.equals(other.nickname)) {
            return false;
        }
        return true;
    }
}
